<?php
/**
 * 经纪人客户互动模型
 */

namespace app\badmin\model;


use app\common\model\BaseModel;

class AgentUserInteractionLog extends BaseModel {

    public static $type_s = [
        1  => '首次查看了你的名片，去跟他打个招呼以建立良好的形象',
        2  => '首次点赞了你的名片',
        3  => '名片视频播放',
        4  => '赞了你的动态',
        5  => '正在浏览你的推荐房源',
        6  => '关注了',
        7  => '分享房源',
        9  => '提交了贷款需求，点击查看',
        10 => '找房需求已更新，查看实时推荐房源',
        11 => '正在浏览你的房源',
        12 => '已帮填找房需求，查看实时推荐房源',
    ];

    /**
     * 互动记录
     * @param $params
     * @return array
     */
    public static function interaction_log($params) {
        $column = ['type,itime'];
        $map[] = ['agent_user_id', '=', $params['id']];
        $map[] = ['type', 'in', [1, 2, 3, 4]];
        $order = ['itime' => 'desc'];
        $data = self::getDatas($map, $column, $order);
        if (empty($data)) {
            $result['count'] = 0;
            $result['page'] = 1;
            $result['pageNum'] = 1;
            $result['data'] = [];
        } else {
            foreach ($data as $k => $v) {
                $data[$k]['day_time'] = date('Y-m-d', $data[$k]['itime']);
                $data[$k]['itime'] = date('H:i:s', $data[$k]['itime']);
                $data[$k]['type'] = self::$type_s[$data[$k]['type']];
            }
            $data = self::treeFilter($data);
            $count = count($data);
            $page1 = (int)$params['page1'];
            $long1 = (int)$params['long1'];
            $page1_ = ($page1 - 1) * $long1;
            $data = array_slice($data, $page1_, $long1);
            if ($long1 > 0) {
                $pageNum = ceil($count / $long1);
            } else {
                $pageNum = 1;
            }
            $result['count'] = $count;
            $result['page'] = $page1;
            $result['pageNum'] = $pageNum;
            $result['data'] = $data;
        }
        return $result;
    }

    /**
     * 互动记录结构
     * @param $data
     * @return array
     */
    public static function treeFilter($data) {
        $tree = [];
        foreach ($data as &$val) {
            $arr = [
                'type'  => $val['type'],
                'itime' => $val['itime'],
            ];
            $tree[$val['day_time']]['day_time'] = $val['day_time'];
            $tree[$val['day_time']]['children'][] = $arr;
        }
        return array_values($tree);
    }

    /**
     * 获取多条
     * @param $map
     * @param $column
     * @return array|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function getDatas($map, $column, $order) {
        return db("agent_user_interaction_log")
            ->where($map)
            ->order($order)
            ->field($column)
            ->select();
    }

    /**
     * 房源记录
     * @param $params
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function house_log($params) {
        $column = ['type,itime,house_id'];
        $map[] = ['agent_user_id', '=', $params['id']];
        $map[] = ['type', 'in', [5, 6, 7, 8, 9, 10, 11, 12]];
        $order = ['itime' => 'desc'];
        $data = self::getDatas($map, $column, $order);
        if (empty($data)) {
            $result['count'] = 0;
            $result['page'] = 1;
            $result['pageNum'] = 1;
            $result['data'] = [];
        } else {
            foreach ($data as $k => $v) {
                $data[$k]['day_time'] = date('Y-m-d', $v['itime']);
                $data[$k]['itime'] = date('H:i:s', $v['itime']);
                $houseName = House::where("id", $v['house_id'])->value('title');
                if ($v['type'] == 8) {
                    $data[$k]['type'] = "询问了{$houseName}的费用情况，点击去计算";
                } elseif ($v['type'] == 9 || $v['type'] == 12) {
                    $data[$k]['type'] = self::$type_s[$v['type']];
                } else {
                    $data[$k]['type'] = self::$type_s[$v['type']] . $houseName;
                }
            }
            $data = self::treeFilter($data);
            $count = count($data);
            $page2 = (int)$params['page2'];
            $long2 = (int)$params['long2'];
            $page2_ = ($page2 - 1) * $long2;
            $data = array_slice($data, $page2_, $long2);
            if ($long2 > 0) {
                $pageNum = ceil($count / $long2);
            } else {
                $pageNum = 1;
            }
            $result['count'] = $count;
            $result['page'] = $page2;
            $result['pageNum'] = $pageNum;
            $result['data'] = $data;
        }
        return $result;
    }

}